package com.guigu.community.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.community.pojo.Suppliers;
import com.guigu.community.service.SuppliersService;
import com.guigu.community.vo.SuppliersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

import static com.fasterxml.jackson.databind.type.LogicalType.DateTime;

@RestController
@RequestMapping("/suppliers")
@CrossOrigin
public class SuppliersController {
    @Autowired
    private SuppliersService suppliersService;

    @PostMapping("/selectAll/{current}/{size}")
    @ResponseBody
    public PageInfo<Suppliers> selectAll(@PathVariable("current") Integer current,
                                         @PathVariable("size") Integer size,
                                         @RequestBody SuppliersVo vo) {
        return suppliersService.queryByVo(current, size, vo);
    }

    @GetMapping("/status/{supplierId}")
    @ResponseBody
    public Byte getSupplierStatus(@PathVariable("supplierId") Integer supplierId) {
        return suppliersService.getSupplierStatusById(supplierId);
    }

    @PostMapping("/approve/{supplierId}")
    @ResponseBody
    public boolean approveSupplier(@PathVariable("supplierId") Integer supplierId) {
        return suppliersService.approveSupplier(supplierId);
    }

    // 新增接口：审核通过并更新 users 表的 Is_vendor
    @PostMapping("/approveAndUpdateUser/{supplierId}")
    @ResponseBody
    public boolean approveSupplierAndUpdateUser(@PathVariable("supplierId") Integer supplierId) {
        return suppliersService.approveSupplierAndUpdateUser(supplierId);
    }

    @PostMapping("/reject/{supplierId}")
    @ResponseBody
    public boolean rejectSupplier(@PathVariable("supplierId") Integer supplierId) {
        return suppliersService.rejectSupplier(supplierId);
    }

    @PostMapping("/selectByStatusZero/{current}/{size}")
    @ResponseBody
    public PageInfo<Suppliers> selectByStatusZero(@PathVariable("current") Integer current,
                                              @PathVariable("size") Integer size) {
        SuppliersVo vo = new SuppliersVo();
        vo.setStatus((byte) 0);
        return suppliersService.queryVo(current, size, vo);
    }

    @PostMapping("/selectByStatusZero1/{current}/{size}")
    @ResponseBody
    public PageInfo<Suppliers> selectByStatusZero1(@PathVariable("current") Integer current,
                                              @PathVariable("size") Integer size) {
        SuppliersVo vo = new SuppliersVo();
        vo.setStatus((byte) 1);
        return suppliersService.queryVo(current, size, vo);
    }


    @PostMapping("/AddSupplier/{uid}")
    public String AddSupplier(@RequestBody Suppliers suppliers, @PathVariable Integer uid){
        suppliers.setIsBusiness(0);
        suppliers.setUserId(uid);//设置用户Id
        suppliers.setBalance(0);//设置账户余额
        suppliers.setStatus((byte) 0);//设置商户状态：0'待审核'
        boolean save = suppliersService.save(suppliers);
        if (save)
            return "yes";
        return "NO";
    }

    @PutMapping("updateSupplier")
    public String updateSupplier(@RequestBody Suppliers suppliers){
        suppliers.setUpdatedAt(new Date());
        boolean update = suppliersService.updateById(suppliers);
        if (update)
            return "yes";
        return "NO";
    }
}